<?php
namespace app\model;

use project\base\Model;

class TasksModel extends Model
{
  protected $table = 'tasks';

  /**
   * 显示任务详情
   *
   * @param string $id 任务的id
   * @return $data id对应任务的所有信息
   */
  public function showDetail($id)
  {

    $where = " WHERE id = '$id' ";
    $data = $this->search($this->table, $where);
    return $data;
  }

  /**
   * 显示所有的方案
   *
   * @return void
   */
  public function showAll()
  {
    $result = " task_title,task_accept,task_input,task_done,id ";
    $where = ' ORDER BY task_done,task_input';
    $data = $this->search($this->table, $where, $result);
    return $data;
  }

  /**
   * 显示项目进度
   *
   * @return void
   */
  public function showProgress()
  {
    $result = " task_accept,task_done ";
    $where = '';
    $data = $this->search($this->table, $where, $result);
    return $data;
  }

  /**
   * 接受任务
   *
   * @param string $id 任务的id
   * @param string $username 用户名
   * @return $data 显示是否接受成功
   */
  public function acceptTask($id, $username)
  {
    $set = " task_accept = '$username'";
    $where = " WHERE id = '$id'";
    $data = $this->update($this->table, $set, $where);
    return $data;
  }

  /**
   * 删除任务
   *
   * @param string $id 任务的id
   * @return $data 显示是否删除成功
   */
  public function deleteTask($id)
  {
    $where = " WHERE id = '$id'";
    $data = $this->delete($this->table, $where);
    return $data;
  }
  /**
   * 放弃任务
   *
   * @param string $id 任务的id
   * @return $data 显示是否接受成功
   */
  public function giveUpTask($id)
  {
    $set = " task_accept = null";
    $where = " WHERE id = '$id'";
    $data = $this->update($this->table, $set, $where);
    return $data;
  }

  /**
   * 完成任务
   *
   * @param string $id 任务的id
   * @return $data 显示是否接受成功
   */
  public function doneTask($id)
  {
    $set = " task_done= 1";
    $where = " WHERE id = '$id'";
    $data = $this->update($this->table, $set, $where);
    return $data;
  }

  /**
   * 发布任务
   *
   * @param array $param 包含了任务标题,信息,发布者
   * @return $data 显示是否接受成功
   */
  public function inputTask($param)
  {
    $col = '';
    $values = '';
    foreach ($param as $key => $value) {
      $col .= $key . ",";
      $values .= '"' . $value . '",';
    }
    $col = rtrim($col, ',');
    $values = rtrim($values, ',');
    $data = $this->add($this->table, $col, $values);
    return $data;
  }



}